module.exports = app => {
    const Router = require("koa-router")
    const router = new Router()
    app.use(router.routes()).use(router.allowedMethods())
    const Data = require("../db/data")
    const bodyParser = require("koa-bodyparser")
    app.use(bodyParser())
    router.get('/data', async ctx => {
        const allData = await Data.find();
        const { page, size } = ctx.query;
        const data = await Data.find().limit(+size).skip(+size*(+page - 1));
        ctx.body = {
            message: "获取成功",
            status: 200,
            total: allData.length,
            data
        }
    })
    router.post('/data', async ctx => {
        let fs = require("fs");
        fs.readFile("C:/Users/Administrator/Desktop/demo.txt", "utf8", (err, data) => {
            if (!err) {
                const db = require("../db/data.js");
                JSON.parse(data).forEach(item => {
                    db.create(item);
                });
            }
            else {
                console.log(err);
            }
        })
        ctx.body = {
            message: "添加成功",
            status: 200,
        }
    })
    router.post('/data/delete', async ctx => {
        const { _id } = ctx.request.body;
        const data = await Data.findOneAndDelete(_id);
        ctx.body = {
            message: "删除成功",
            status: 200,
            data
        }
    })
    router.get('/data/:_id', async ctx => {
        const { _id } = ctx.params;
        const data = await Data.findOne({
            _id
        });
        ctx.body = {
            message: "获取成功",
            status: 200,
            data
        }
    })
}